<?php
error_reporting(E_ALL);
ini_set("display_errors", "On");
ini_set("log_errors", "Off");

/**
 * Sets up the test environment by loading required libraries such as SimpleTest.
 */

function loadDir($dir, $fhandler) {
	if (is_dir($dir) && $handle = opendir($dir)) {
		while (false !== ($file = readdir($handle))) {
			$filefull = $dir.DIRECTORY_SEPARATOR.$file;
			if ($file != "." && $file != "..") {
				if(!loadDir($filefull, $fhandler)) {
					$fhandler -> handle($filefull);
				}
			}
		}
		closedir($handle);
		return true;
	} else {
		return false;
	}
}

function loadLibDir($libName, $fhandler) {
	loadDir("..".DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR.$libName, $fhandler);
}

function loadClassDir($fhandler){
	$loadClass = FALSE;
	foreach (glob("..\model") as $filename){
		$loadClass = TRUE;
		loadDir($filename, $fhandler);
	}

	if(!$loadClass){
		foreach (glob("..\model") as $filename){
			loadDir($filename, $fhandler);
		}
	}
}

class PHPFileHandler {
	function handle($file) {
		if($this -> endswith($file, ".php")) {
			echo $file;
			echo "<br>";
		}
	}

	function endswith($string, $test) {
		$strlen = strlen($string);
		$testlen = strlen($test);
		if ($testlen > $strlen) return false;
		return substr_compare($string, $test, -$testlen) === 0;
	}
}

class PHPLoaderHandler extends PHPFileHandler {
	function handle($file) {
		if($this -> endswith($file, ".php")) {
			require_once $file;
		}
	}
}

class SimpleTestLibHandler extends PHPFileHandler {
	function handle($file) {
		if($this -> endswith($file, "autorun.php")) {
			require_once $file;
		}
	}
}

$phploader = new PHPLoaderHandler();
$simpleTestLibLoader = new SimpleTestLibHandler();

// load simpletest library
loadLibDir('simpletest', $simpleTestLibLoader);
// load symfony lime test lib
loadLibDir('lime', $phploader);
// load sitetalk classes
loadClassDir($phploader);
// load test tools
//loadDir(__DIR__.DIRECTORY_SEPARATOR.'TestData', $phploader);
loadDir('..'.DIRECTORY_SEPARATOR.'test/bart-system', $phploader);

?>
<?php if (PHP_SAPI !== "cli"): ?>
<a href="index.php">Back to Report</a>
<?php endif ?>
